import draggable from "vuedraggable"
<template>
  <div class="app-container">
    <div>
      <el-row align="middle" type="flex" :gutter="10">
        <span style="width: 200px;font-weight: bold;font-size: 18px">模板广场</span>
        <el-col/>
        <el-button type="primary" v-hasPermi="['saveFormProjectTemplate']"  icon="el-icon-folder-add" @click="handleAdd">新增模板</el-button>
      </el-row>
    </div>

    <div class="wrap">
      <el-form ref="form">
        <div class="box">
          <div class="inputBox">
            <input type="text" v-model="queryParams.projectName" placeholder="请输入模板名称检索" autofocus="autofocus"/>
          </div>
          <div class="searchBtn" @click="handleQuery">搜模板</div>
        </div>
      </el-form>
    </div>

    <el-row :gutter="10" class="mb8">
      <div>
        <el-tabs v-model="queryParams.projectTypeCode" @tab-click="handleTabActiveClick">
          <el-tab-pane label="私有模板" name="FORM_SURVEY_PRIVATE_TEMPLATE">
            <el-card class="header-container mt10" shadow="never">
              <!--分类筛选 开始-->
              <div class="cate_sort">
                <dl>
                  <dt>分类：</dt>
                  <dd><a @click="handleCategoryIdClick(undefined)"  :class="{'hover':queryParams.categoryId===''}">全部</a></dd>
                  <dd v-for="item in categoryList" :key="item.categoryId" ><a @click="handleCategoryIdClick(item.categoryId)"  :class="{'hover':queryParams.categoryId===item.categoryId}">{{item.categoryName}}</a></dd>
                </dl>
              </div>
            </el-card>
          <div class="mt20"></div>
            <el-card class="header-container" shadow="never">
             <!--beg beg-->
              <div class="project-grid-container">
                <div class="project-grid-view">
                  <div v-for="template in templateDataList" :key="template.projectId" class="project-template-view">
                    <img :src="template.coverImg" style="width: 100%; height: 133px;">
                    <p class="project-template-title">
                      {{ template.projectName }}
                    </p>
                    <p style="color: rgba(189, 188, 188, 100); font-size: 12px; margin: 0;">
                      {{ template.createTime }}
                    </p>
                    <div class="project-template-use-view">
                      <div class="project-template-use-view-btn">
                        <el-button  icon="el-icon-view" class="button-class"  type="text" @click="toProjectTemplatePreview(template.projectId)">查看</el-button>
                        <el-button  icon="el-icon-setting" class="button-class"  v-if="checkPermi(['updateFormProjectTemplate'])"   type="text" @click="toProjectHandle(template.projectId,'editorTemplate')">设计</el-button>
                        <el-button  icon="el-icon-setting" class="button-class"  v-if="!checkPermi(['updateFormProjectTemplate'])"  type="text"  @click="noPermission('暂无设计权限!')">设计</el-button>
                        <br>
                        <el-button  icon="el-icon-edit" class="button-class" v-if="checkPermi(['updateFormProjectTemplate'])" type="text" @click="handleUpdate(template)">编辑</el-button>
                        <el-button  icon="el-icon-delete" class="button-class" v-if="checkPermi(['deleteFormProjectTemplate'])"  type="text" @click="handleDelete(template)">删除</el-button>

                        <el-button  icon="el-icon-edit" class="button-class"  v-if="!checkPermi(['updateFormProjectTemplate'])"  type="text" @click="noPermission('暂无编辑权限!')">编辑</el-button>
                        <el-button  icon="el-icon-delete" class="button-class" v-if="!checkPermi(['deleteFormProjectTemplate'])"  type="text" @click="noPermission('暂无删除权限!')">删除</el-button>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div>
                <pagination v-show="total>0"  :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows" @pagination="queryFormProjectTemplatePageResList"/>
              </div>
              <!--无任何满足条件的模板提示-->
              <div class="field-box" v-if="total===0">
                <data-empty  :desc="'无任何满足条件的模板'" />
              </div>
      <!--end end-->
            </el-card>
          </el-tab-pane>
          <el-tab-pane label="公共模板" name="FORM_SURVEY_PUBLIC_TEMPLATE">
            <el-card class="header-container mt10" shadow="never">
              <!--分类筛选 开始-->
              <div class="cate_sort">
                <dl>
                  <dt>分类：</dt>
                  <dd><a @click="handleCategoryIdClick(undefined)"  :class="{'hover':queryParams.categoryId===''}">全部</a></dd>
                  <dd v-for="item in categoryList" :key="item.categoryId" ><a @click="handleCategoryIdClick(item.categoryId)"  :class="{'hover':queryParams.categoryId===item.categoryId}">{{item.categoryName}}</a></dd>
                </dl>
              </div>
            </el-card>
            <div class="mt20"></div>
            <el-card class="header-container" shadow="never">
              <!--beg beg-->
              <div class="project-grid-container">
                <div class="project-grid-view">
                  <div v-for="template in templateDataList" :key="template.projectId" class="project-template-view">
                    <img :src="template.coverImg" style="width: 100%; height: 133px;">
                    <p class="project-template-title">
                      {{ template.projectName }}
                    </p>
                    <p style="color: rgba(189, 188, 188, 100); font-size: 12px; margin: 0;">
                      {{ template.hits }}人喜欢
                    </p>
                    <div class="project-template-use-view">
                      <div class="project-template-use-view-btn" @click="toProjectTemplatePreview(template.projectId)">
                        <div>
                          <font-icon class="fa fa-eye" style="font-size: 40px;" />
                        </div>
                        <el-button type="text" class="button-class" >查看</el-button>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div>
                <pagination v-show="total>0"  :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows" @pagination="queryFormProjectTemplatePageResList"/>
              </div>
              <!--无任何满足条件的模板提示-->
              <div class="field-box" v-if="total===0">
                <data-empty  :desc="'无任何满足条件的模板'" />
              </div>
              <!--end end-->
            </el-card>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-row>


    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="addUpdateOpen" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="addUpdateOpenRules"  label-width="80px">
        <el-form-item label="模板分类" prop="categoryId">
          <el-select v-model="form.categoryId" clearable style="width: 100%;" placeholder="请选择">
            <el-option
              v-for="item in categoryList"
              :key="item.categoryId"
              :label="item.categoryName"
              :value="item.categoryId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="模板名称" prop="projectName">
          <el-input :hide-required-asterisk="true" autocomplete="off" v-model="form.projectName" placeholder="请输入模板名称,50个字符长度" maxlength="50" />
        </el-form-item>

        <el-form-item label="模板封面" prop="coverImg" ref="coverImg">
          <el-upload
            ref="image"
            class="avatar-uploader"
            action="#"
            :auto-upload="false"
            accept="image/jpeg,image/gif,image/png,image/jpg"
            :on-change="requestUpload"
            :show-file-list="false">
          <img v-if="form.coverImg" :src="form.coverImg"  class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelAddUpdateOpen">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import DataEmpty from '@/components/DuoJuHe/Form/DataEmpty'
  import {queryNormalSelectFormCategoryResList } from '@/api/form/formCategory'
  import {queryFormProjectPrivateTemplatePageResList, queryFormProjectPublicTemplatePageResList, saveFormProjectTemplate, updateFormProjectTemplate,deleteFormProjectTemplateByProjectId} from '@/api/form/formProjectTemplate'
  //默认查询参数
  let defaultQueryParams = {
    projectName: undefined,
    categoryId: "",
    projectTypeCode: "FORM_SURVEY_PRIVATE_TEMPLATE",
    page: 1,
    rows: 10
  }
  export default {
    name: 'formTemplateAdmin',
    components: {
      DataEmpty
    },
    data() {
      return {
        // 查询参数
        queryParams: { ...defaultQueryParams },
        //总条数
        total: 0,
        //模板分类
        categoryList:[],
        //公有数据内容
        templateDataList: [],
        // 弹出层标题
        title: "",
        // 是否显示新增修改弹出层
        addUpdateOpen: false,
        // 增加修改表单参数
        form: {},
        // 新增修改表单校验
        addUpdateOpenRules: {
          projectName: [
            { required: true, message: "模板名称不能为空",trigger: "blur" },
          ],
          coverImg: [
            { required: true, message: "封面图片必须上传"},
          ],
          categoryId: [
            { required: true, message: "模板分类必须选择"},
          ]
        },
      }
    },
    computed: {},
    created() {
      this.queryNormalSelectFormCategoryResList()
      this.queryFormProjectTemplatePageResList()
    },
    methods: {
      handleTabActiveClick(tab, event) {
        this.queryFormProjectTemplatePageResList();
      },

      handleCategoryIdClick(categoryId) {
        if (categoryId===undefined){
          categoryId = "";
        }
        this.queryParams.categoryId = categoryId;
        this.queryFormProjectTemplatePageResList();
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1
        this.queryFormProjectTemplatePageResList()
      },

      /** 查询模板分类 */
      queryNormalSelectFormCategoryResList() {
        queryNormalSelectFormCategoryResList({}).then(res => {
            this.categoryList = res.data
          }
        )
      },

      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.queryNormalSelectFormCategoryResList();
        this.addUpdateOpen = true;
        this.title = "新增模板";
      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.queryNormalSelectFormCategoryResList();
        //修改表单填充
        this.form = {...row};
        this.addUpdateOpen = true;
        this.title = "修改模板";
      },

      // 取消修改增加弹出层按钮
      cancelAddUpdateOpen() {
        this.addUpdateOpen = false
      },

      //跳转到项目
      toProjectHandle(key, type) {
        this.$router.push({path: `/project/form/template/${type}`, query: {key: key, active: type}})
        //this.$router.push({path: `/project/form/workflow/editorWorkflow`, query: {key: key, active: type}})
      },
      //c查看模板
      toProjectTemplatePreview(key) {
        this.$router.push({path: '/project/template/preview', query: {key: key}})
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除所选模板?', "警告", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning"
        }).then(function() {
          const projectId={projectId:row.projectId}
          return deleteFormProjectTemplateByProjectId(projectId);
        }).then(response => {
          this.$modal.msgSuccess(response.message)
          this.queryFormProjectTemplatePageResList();
        }).catch(function() {});
      },

      /** 提交保存按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.projectId) {
              updateFormProjectTemplate(this.form).then(response => {
                this.$modal.msgSuccess(response.message)
                this.queryFormProjectTemplatePageResList();
                this.addUpdateOpen = false
              });
            }else {
              saveFormProjectTemplate(this.form).then(response => {
                this.$modal.msgSuccess(response.message)
                this.addUpdateOpen = false
                this.toProjectHandle(response.data,'editorTemplate')
              });
            }
          }
        });
      },
      // 表单重置
      reset() {
        this.form = {
          projectId: undefined,
          projectName: undefined,
          coverImg: undefined,
          categoryId: undefined
        };
        this.resetForm('form')
      },
      //上传封面图
      requestUpload(file) {
        let that = this
        const typeArr = ['image/png', 'image/gif', 'image/jpeg', 'image/jpg'];
        const isJPG = typeArr.indexOf(file.raw.type) !== -1;
        if (!isJPG) {
          this.$message.error('只能是图片!');
          return;
        }
        const isLt1M = file.size / 1024 / 1024 < 1;
        if (!isLt1M) {
          this.$message.error('上传模板封面大小不能超过 1M!');
          return
        }
        let reader = new FileReader();
        reader.readAsDataURL(file.raw);
        reader.onload = function(e){
          that.form = {
            ...that.form,
            coverImg: this.result
          }
          that.$refs["coverImg"].clearValidate()
        }
      },

      /** 查询项目模板发送记录列表 */
      queryFormProjectTemplatePageResList() {
        if("FORM_SURVEY_PUBLIC_TEMPLATE"===this.queryParams.projectTypeCode){
          queryFormProjectPublicTemplatePageResList(this.queryParams).then(res => {
              this.templateDataList = res.data.records
              this.total = res.data.total
            }
          )
        }else {
          queryFormProjectPrivateTemplatePageResList(this.queryParams).then(res => {
              this.templateDataList = res.data.records
              this.total = res.data.total
            }
          )
        }
      },
    }
  }
</script>

<style lang="less" scoped>
  .wrap {
    width: 100%;
    display: flex;
    justify-content: center;
  }

  .wrap .box {
    display: flex;
    margin-top: 40px;
    align-items: center;
  }

  .inputBox {
    position: relative;
  }

  .wrap .box .inputBox input {
    margin: 0;
    width: 480px;
    height: 40px;
    border: 2px solid #c4c7ce;
    border-radius: 10px 0 0 10px;
    border-right: 0;
    overflow: visible;
    padding-left: 10px;
  }

  .wrap .box .inputBox input:focus {
    outline: none;
    border: 2px solid #4e6ef2;
    border-right: 0;
  }

  .wrap .box .inputBox .iptClass {
    border-radius: 10px 0 0 0;
  }

  .wrap .searchBtn {
    cursor: pointer;
    width: 112px;
    height: 40px;
    line-height: 40px;
    line-height: 40px \9;
    background-color: #4e6ef2;
    border-radius: 0 10px 10px 0;
    font-size: 17px;
    box-shadow: none;
    font-weight: 400;
    border: 0;
    outline: 0;
    letter-spacing: normal;
    text-align: center;
    color: #fff;
  }

  .cate_sort {
    margin: 0 auto;
    background: #fff;
  }
  .cate_sort dl {
    display: block;
    height: 40px;
    clear: both;
    border-bottom:#F5F5F5 dashed 1px;
  }


  .cate_sort dl dt {
    display: block;
    height: 35px;
    font-size: 14px;
    float: left;
    width: 50px;
    line-height: 35px;
    font-weight: bold;
    color: #333
  }

  .cate_sort dl dd {
    display: block;
    height: 35px;
    float: left;
    text-align: center;
    line-height: 35px;
    padding: 0 15px 0 15px;
  }

  .cate_sort dl dd a:hover {
    background: #4e6ef2;
    color: #FFF;
    padding: 3px 5px 3px 5px;
    border: #4e6ef2 solid 1px;
    border-radius: 10px;
  }

  .cate_sort dl dd a.hover {
    background: #4e6ef2;
    color: #FFF;
    padding: 3px 5px 3px 5px;
    border: #4e6ef2 solid 1px;
    border-radius: 10px;
  }

  .cate_sort dl dd a {
    font-size: 14px;
    padding: 3px 5px 3px 5px;
    border: #fff solid 1px;
  }
  .project-grid-view {
    display: flex;
    position:relative;
    width: 1450px;
    text-align: center;
    margin:0 auto;
    flex-direction: row;  /*横向*/
    align-items: center; /*纵轴居中*/
    flex-wrap: wrap;
    clear: both;
  }
  .project-template-title {
    color: rgba(16, 16, 16, 100);
    font-size: 14px;
    margin: 0 3px;
    line-height: 30px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .project-template-view {
    float:left;
    width: 250px;
    height: 200px;
    line-height: 20px;
    border-radius: 10px;
    text-align: center;
    overflow: hidden;
    margin: 20px;
    border: 1px solid rgba(187, 187, 187, 100);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
    background: white;
    position: relative;
  }
  .project-template-view:hover .project-template-use-view {
    display: block;
    cursor: pointer;
  }
  .project-template-use-view {
    position: absolute;
    width: 100%;
    height: 200px;
    z-index: 100;
    margin: 0;
    padding: 0;
    background-color: #f0f0f0;
    bottom: 0;
    filter: alpha(opacity=90);
    -moz-opacity: 0.9;
    opacity: 0.9;
    display: none;
    border: none;
  }

  .project-template-use-view-btn {
    margin-top: 30%;
  }

  .button-class{
    font-size: 18px;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    line-height: 178px;
    display: block;
  }
</style>
